//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    Scaned: '扫描二维码',
    team: '队伍名称',
    score: 98,
    hasTeam: false,
    isAdmin: false,
    data: {},
    url:app.globalData.url+'/wxapp'
  },
  Admin1: function() {
    wx.navigateTo({
      url: '../admin/adminscan',
    })
  },
  Admin2: function() {
    wx.navigateTo({
      url: '../admin/admin',
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  refresh: function () {
    let that = this;
    app.login().then(function () {
      console.log(app.globalData.data)
      that.setData({
        data: app.globalData.data,
      })
      if (!that.data.data.name) {
        wx.navigateTo({
          url: '../register/register',
        })
        return
      }
      else if (!that.data.data.isTeamed) {
        wx.navigateTo({
          url: '../newteam/newteam',
        })
      }

    })
  },
  onShow:function(){
    this.refresh()
  },
  onLoad: function() {
    let that = this;
    // app.login().then(function() {
    //   console.log(app.globalData.data)
    //   that.setData({
    //     data: app.globalData.data,
    //   })
    //   if (!that.data.data.name) {
    //     wx.navigateTo({
    //       url: '../register/register',
    //     })
    //   }
    //   if (!that.data.data.isTeamed) {
    //     wx.navigateTo({
    //       url: '../newteam/newteam',
    //     })
    //   }

    // })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })

    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          Bmob.User.upInfo(res.userInfo)
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  anaCode: function(str) {
    res = JSON.parse(str)
    // objectId:该二维码在服务端的id
    // operation:进行的操作 add subtract clear
    // score:操作的分数

  },
  Scan: function() {
    let that = this
    wx.scanCode({
      onlyFromCamera: true,
      scanType: 'qrCode',
      success: (res) => {
        // 成功扫码
        console.log('扫码结果：', res)
        that.setData({
          Scaned: res.result
        })
        wx.navigateTo({
          url: '../showCode/showCode?str=' + res.result,
        })
        // that.anaCode(res.result)
      }
    })
  },
  Show: function() {
    let that = this
    wx.switchTab({
      url: '../show/show',
    })
  },
  Rank: function() {
    let that = this
    wx.switchTab({
      url: '../rank/rank',
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    Bmob.User.upInfo(e.detail.userInfo)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})